import DTO.User;
import java.io.*;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "ServletD",urlPatterns ="/ServletD")
public class ServletD extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String str=username+" "+password;
        FileReader inOne=new FileReader("infor.txt");
        BufferedReader in=new BufferedReader(inOne);
        String s=null;
        while((s=in.readLine())!=null){
            String p=s.substring(0,str.length());
            if(str.equals(p)==true){
                String a=s.substring(str.length()+1);
                String[]strs=a.split(" ");
                String email=strs[1];
                String phone=strs[0];
                User user=new User(username,password,email,phone);
                request.getSession().setAttribute("user",user);
                response.sendRedirect("http://localhost:8080/infor.jsp");
            }
        }
        if((s=in.readLine())==null) response.sendRedirect("http://localhost:8080/error.jsp");
        in.close();
        inOne.close();

    }
}
